package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ck
 * @date 2021/10/14 16:18
 */
@RestController
@RequestMapping("/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @PostMapping
    @CacheEvict(value = "dishCache",allEntries = true)
    @ApiOperation("新增菜品接口")
    public R<String> save(@RequestBody DishDto dishDto){
        log.info("dishDto:{}",dishDto);
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "菜品名称",required = false)
    })
    @Cacheable(value = "dishCache",key = "#page+'_'+#pageSize")
    public R<Page> page(int page,int pageSize,String name){
        R<Page> pageR = dishService.pageCat(page, pageSize, name);

        return pageR;
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品接口")
    @Cacheable(value = "dishCache",key = "#id")
    public R<DishDto> get(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        if (dishDto != null){
            return R.success(dishDto);
        }else {
            return R.error("没有找到对象");
        }
    }

    @PutMapping
    @ApiOperation("修改菜品接口")
    @CacheEvict(value = "dishCache",allEntries = true)
    public R<DishDto> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success(dishDto);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("更改菜品状态接口")
    @CacheEvict(value = "dishCache",allEntries = true)
    public R<String> updateStatus(@PathVariable int status, @RequestParam List<Long> ids){
        dishService.updateWithStatus(status,ids);
        return R.success("修改成功");
    }

    @DeleteMapping
    @Transactional
    @ApiOperation("删除菜品接口")
    @CacheEvict(value = "dishCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        dishService.deleteByIds(ids);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    @ApiOperation("获取所有菜品接口")
    @Cacheable(value = "dishCache",key = "#dish.getCategoryId()+'_'+#dish.status")
    public R<List<DishDto>> list(Dish dish) {
        R<List<DishDto>> listR = dishService.catList(dish);
        return listR;
    }
}
